package com.snowflakes.gruntr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;

public class GruntList extends Activity {

	private ParseQueryAdapter<ParseObject> gruntAdapter;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.grunts);

		Intent i = getIntent();
		Bundle extras = i.getExtras();

		String id = extras.getString("id");

		gruntAdapter = new GruntAdapter(this, id);

		listView = (ListView) findViewById(R.id.lvGrunts);
		listView.setAdapter(gruntAdapter);
		gruntAdapter.loadObjects();

		/*if (gruntAdapter.isEmpty()) {
				Toast.makeText(getBaseContext(), "No Grunts to Show",
						Toast.LENGTH_SHORT).show();
				finish();	
		}*/

	}

	public void play(View view) {

		ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Grunt");

		String id = (String) view.getTag();

		query.whereEqualTo("objectId", id);

		ParseObject request = null;

		try {
			request = query.getFirst();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (request != null) {
				ParseFile sound = request.getParseFile("Sound");

				File temp3GP = null;
				try {
					temp3GP = File.createTempFile("temp_audio", "3gp",
							getCacheDir());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				temp3GP.deleteOnExit();

				FileOutputStream fos = null;
				try {
					fos = new FileOutputStream(temp3GP);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					fos.write(sound.getData());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				MediaPlayer mediaPlayer = new MediaPlayer();

				FileInputStream fis = null;
				try {
					fis = new FileInputStream(temp3GP);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					mediaPlayer.setDataSource(fis.getFD());
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					mediaPlayer.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mediaPlayer.start();

			}

		}
	}
}
